import QtQuick 2.0
import QtQuick.Controls 1.1
import SmartingEEGClientAgent 1.0
import Plot 1.0

Rectangle
{
    id: root

    SmartingEEGClientAgent
    {
        id: eegAgent
        objectName: "eegAgent"
    }

    Plot
    {
        id: plot
        objectName: "plot"
        visible: false

        width: root.width
        height: root.height
    }

    ListModel
    {
        id: deviceModel
    }

    Connections
    {
        target: eegAgent
        onServerDiscovered:
        {
            console.log("Server Discovered: " + name);
            //if(name === "SMARTING")
            //{
                deviceModel.append({"name": name, "address": address})
            //}
        }
            onConnectedToServer:
        {
            startDiscoveryBtn.visible = false;
            startAcquisitionBtn.visible = true;
            deviceView.visible = false;
            console.log("CONNECTED");
        }
        onDisconnectedFromServer: console.log("DISCONNECTED")
    }

    Column
    {
        id: btnColumn

        Button
        {
            id: startDiscoveryBtn
            text: "Start discovery"
            width: root.width
            height: root.height / 6

            onClicked:
            {
                eegAgent.stopServerDiscovery()
                startDiscoveryDelay.start()
            }

            Timer
            {
                id: startDiscoveryDelay
                interval: 500
                running: false
                repeat: false
                onTriggered:
                {
                    deviceModel.clear()
                    eegAgent.startServerDiscovery()
                }
            }
        }

        Button
        {
            id: startAcquisitionBtn
            text: "Start acquisition"
            width: root.width
            height: root.height / 6
            visible: false

            onClicked:
            {
                plot.visible = true;
                btnColumn.visible = false;
                //eegAgent.enableChannel(1);
                //eegAgent.setMode(SmartingEEGClientAgent.TEST_MODE);
                eegAgent.startAcquisition();
            }
        }
    }

    ListView
    {
        id: deviceView
        width: root.width
        anchors.top: btnColumn.bottom
        model: deviceModel
        delegate: Component
        {
            id: deviceDelegate

            Item
            {
                width: root.width
                height: col.height

                Column
                {
                    id: col
                    spacing: 3

                    Text
                    {
                        id: nameTxt
                        text: "<b>Name:</b> " + name
                    }

                    Text
                    {
                        id: addressTxt
                        text: "<b>Address:</b> " + address
                    }
                }

                MouseArea
                {
                    anchors.fill: parent
                    onClicked: eegAgent.connectToServer(address)
                }
            }
        }

        highlight: Rectangle
        {
            color: "lightsteelblue"
            border.color: "black"
            border.width: 3
        }
    }
}
